package ca.polymtl.rubikcube.CameraActivity;

import java.io.IOException;
import java.util.List;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.ImageFormat;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.hardware.Camera.Size;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.SurfaceHolder.Callback;
import android.widget.Toast;

public class CameraSurfaceView extends SurfaceView implements Callback {
	final private static String LOG_TAG = CameraSurfaceView.class.getSimpleName();

	private SurfaceHolder holder;
	private Camera camera;
	public Canvas canvas;

	public CameraSurfaceView(Context context) {
		super(context);

		this.holder = this.getHolder();
		this.holder.addCallback(this);
		Log.d("CUBE", "Constructeur A");
	}

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		this.canvas = canvas;
	}

	public CameraSurfaceView(Context context, AttributeSet attrs) {
		super(context, attrs);
		Log.d("CUBE", "Constructeur B");
		this.holder = this.getHolder();
		this.holder.addCallback(this);
		holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

		// TODO Auto-generated constructor stub
	}

	public CameraSurfaceView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		Log.d("CUBE", "Constructeur C");
		this.holder = this.getHolder();
		this.holder.addCallback(this);
		// TODO Auto-generated constructor stub
	}

	public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
		if (camera == null) {
			return;
		}

		Camera.Parameters parameters = camera.getParameters();
		Log.d("CUBE", "Dim " + width + "," + height);

		List<Size> supportedPreviewSizes = parameters.getSupportedPreviewSizes();
		for (Size size : supportedPreviewSizes) {
			Log.d("CUBE", "Supports " + size.width + "x" + size.height);
		}

		List<Integer> supportedPictureFormats = parameters.getSupportedPictureFormats();

		for (Integer i : supportedPictureFormats) {
			switch (i) {
			case ImageFormat.JPEG:
				Log.d(LOG_TAG, "Supported format: JPEG");
				break;
			case ImageFormat.NV16:
				Log.d(LOG_TAG, "Supported format: NV16");
				break;
			case ImageFormat.NV21:
				Log.d(LOG_TAG, "Supported format: NV21");
				break;
			case ImageFormat.RGB_565:
				Log.d(LOG_TAG, "Supported format: RGB_565");
				break;
			case ImageFormat.YUY2:
				Log.d(LOG_TAG, "Supported format: YUY2");
				break;
			}
		}

		List<Integer> supportedPreviewFormats = parameters.getSupportedPreviewFormats();

		for (Integer i : supportedPreviewFormats) {
			switch (i) {
			case ImageFormat.JPEG:
				Log.d(LOG_TAG, "Supported preview format: JPEG");
				break;
			case ImageFormat.NV16:
				Log.d(LOG_TAG, "Supported preview format: NV16");
				break;
			case ImageFormat.NV21:
				Log.d(LOG_TAG, "Supported preview format: NV21");
				break;
			case ImageFormat.RGB_565:
				Log.d(LOG_TAG, "Supported preview format: RGB_565");
				break;
			case ImageFormat.YUY2:
				Log.d(LOG_TAG, "Supported preview format: YUY2");
				break;
			}
		}

		parameters.setPreviewFormat(ImageFormat.YUY2);
		parameters.setPreviewSize(848, 480);

		// parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
		parameters.setFlashMode(Parameters.FLASH_MODE_OFF);

		camera.setParameters(parameters);
		camera.setDisplayOrientation(90);

		camera.startPreview();
	}

	public void surfaceCreated(SurfaceHolder holder) {
		this.camera = Camera.open();

		if (this.camera == null) {
			Toast.makeText(getContext(), "Unable to open camera", 4).show();

			return;
		}

		try {
			this.camera.setPreviewDisplay(this.holder);

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace(System.out);
		}

	}

	public void surfaceDestroyed(SurfaceHolder holder) {
		if (camera != null) {
			camera.stopPreview();
			camera.release();
			camera = null;
		}
	}

	public Camera getCamera() {
		return camera;
	}

}
